var FormWizard = function () {
    return {
        init: function () {
            if (!jQuery().bootstrapWizard) {
                return;
            }
		

            var wizform = $('#wizForm');
			var alert_success = $('.alert-success', wizform);
            var alert_error = $('.alert-danger', wizform);
            
			/*-----------------------------------------------------------------------------------*/
			/*	Validate the form elements
			/*-----------------------------------------------------------------------------------*/
            wizform.validate({
                doNotHideMessage: true,
				errorClass: 'error-span',
                errorElement: 'span',
                rules: {
                	  /* 选择分类 */
                	categoryId:{
                		required:true
                	},
                	
                    /* Create Account */
					productName: {
                        required: true                        
                    },
                    productCode: {
                        required: true                        
                    },
                    productSalePrice: {
                        required: true
                    },
                    productOriginalPrice: {
                        required: true
                    },
                    unitName: {
                        required: true
                    },
                    shortName: {
                        required: true
                    },
					group: {
                        required: true
                    },
                    brand: {
                        required: true
                    },
                    promotionType: {
                        required: true
                    },
					supplier:{
						required:true
					},
					style:{
						required:true
					},
					shareRate:{
						required:true
					}
                },

                invalidHandler: function (event, validator) { 
                    alert_success.hide();
                    alert_error.show();
                },

                highlight: function (element) { 
                    $(element)
                        .closest('.form-group').removeClass('has-success').addClass('has-error'); 
                },

                unhighlight: function (element) { 
                    $(element)
                        .closest('.form-group').removeClass('has-error'); 
                },

                success: function (label) {
                    if (label.attr("for") == "gender") { 
                        label.closest('.form-group').removeClass('has-error').addClass('has-success');
                        label.remove(); 
                    } else { 
                        label.addClass('valid') 
                        .closest('.form-group').removeClass('has-error').addClass('has-success'); 
                    }
                }
            });
            /*-----------------------------------------------------------------------------------*/
			/*	Initialize Bootstrap Wizard
			/*-----------------------------------------------------------------------------------*/
            $('#formWizard').bootstrapWizard({
                'nextSelector': '.nextBtn',
                'previousSelector': '.prevBtn',
                onNext: function (tab, navigation, index) {
                	
                    if (wizform.valid() == false) {
                        return false;
                    }
                    var total = navigation.find('li').length;
                    var current = index + 1;
                   
                   //如果当前是第二页，需要判断一下尺码及主图是否已经维护
                    if(current==2)
                    {
                    	$("#categoryName").html($("#category3_select option:selected").text());
                    	if($("#category3_select").val()=="")
                    	{
                    		$.alert("请选择正确分类");
                    		return false;
                    	}
                    		
                    }
                    if(current==3)
                    {
                    	if($(".table tbody tr").length==0){
                    		$.alert("请输入尺码信息");
                    		return false;
                    	}
                    	//判断销售 价是否大于0
      				  if(parseFloat($("input[name=productSalePrice]").val())-0==0){
      				  	$.alert("商品销售价不能为0");
      				  	return false;
      				  }
                    	var isInput=true;
                    	var price=true;
                    	$(".table tbody tr").each(function(){
                    		$(this).find("td input").each(function(){
                    			if($(this).val()=="")
                    				{
                    				  isInput=false;
                    					return true;
                    				}            				
                    		
                    		});
                    		if(parseFloat($(this).find("input[name=salesPrice]").val())-0==0){
                    			price=false;
                    			
            					return true;
                    		}
                    		if(!isInput)return true;                 		
                    		
                    	});
                    	
                    	if(!isInput){
                    		$.alert("请检查表格中的必输项");
                    		return false;
                    	}
                    	if(!price){
                    		$.alert("SKU销售价不能为0");
                    		return false;
                    	}
                    	//判断主图 
                    	if($("#nestable2").find("img").length==0) 
                    	{
                    		$.alert("请至少上次一张商品主图");
                    		return false;
                    	}
                    	//判断主图数量
                    	if($("#nestable2").find("img").length>5)
                    	{
                    		$.alert("商品主图最多支持5张");
                    		return false;
                    	}
                    	                   	                   	
                    	
                    }
                    jQuery('li', $('#formWizard')).removeClass("done");
                    var li_list = navigation.find('li');
                    for (var i = 0; i < index; i++) {
                        jQuery(li_list[i]).addClass("done");
                    }
                    if (current == 1) {
                        $('#formWizard').find('.prevBtn').hide();
                      
                        if($("#category3_select").val()=="")
                        {
                        	$.alert("请选择分类");
                        }
                    } else {
                        $('#formWizard').find('.prevBtn').show();
                    }
                    if (current >= total) {
                        $('#formWizard').find('.nextBtn').hide();
                        $('#formWizard').find('.submitBtn').show();
                    } else {                    	
                    	$(".wizard-content").css("height", $("#baseInfo").height() + 240);
                        $('#formWizard').find('.nextBtn').show();
                        $('#formWizard').find('.submitBtn').hide();
                    }
                },
                onPrevious: function (tab, navigation, index) {
                    alert_success.hide();
                    alert_error.hide();
                    var total = navigation.find('li').length;
                    var current = index + 1;
                   
                    jQuery('li', $('#formWizard')).removeClass("done");
                    var li_list = navigation.find('li');
                    for (var i = 0; i < index; i++) {
                        jQuery(li_list[i]).addClass("done");
                    }
                    if (current == 1) {
                        $('#formWizard').find('.prevBtn').hide();
                    } else {
                        $('#formWizard').find('.prevBtn').show();
                    }
                    if (current >= total) {
                        $('#formWizard').find('.nextBtn').hide();
                        $('#formWizard').find('.submitBtn').show();
                    } else {
                        $('#formWizard').find('.nextBtn').show();
                        $('#formWizard').find('.submitBtn').hide();
                    }
                },
				onTabClick: function (tab, navigation, index) {                   
                    return false;
                },
                onTabShow: function (tab, navigation, index) {
                    var total = navigation.find('li').length;
                    var current = index + 1;
                    var $percent = (current / total) * 100;
                    $('#formWizard').find('.progress-bar').css({
                        width: $percent + '%'
                    });
                }
            });

            $('#formWizard').find('.prevBtn').hide();
           
        }
    };
}();